package com.dingding.baseservice.push.order.publishorder;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.request.ReqPublishAboutCar;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.ToolUtil;

public class AddOrderUserInfo extends AbstractPublishOrderHandler {

	private static final Logger log = Logger.getLogger(AddOrderUserInfo.class);

	/**
	 * 
	 * @Title: publishOrderExcute 
	 * @Description: 持久化用户信息到数据库(发布订单)
	 * @param publishAboutCar客户端请求封装bean
	 * @param dataResponse 服务端响应封装bean
	 * @since 1.8.0
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void publishOrderExcute(ReqPublishAboutCar publishAboutCar, DataResponse dataResponse) {

		Userordert userordert = new Userordert();
		// 将客户端请求数据bean转换成数据库保存bean
		convertBean(publishAboutCar, userordert);
		// 订单主键 ID(时间戳+6位随机数)
		String oid = DateFormatUtil.getTimeInMillis().toString() + ToolUtil.generateCellPhoneValNum();
		// 订单ID
		userordert.setOid(oid);
		// 订单状态 1：待抢单
		userordert.setOrderStatus(1);
		// 下单时间
		userordert.setCreateTime(new Date());

		try {

			// 保存数据到数据库
			super.userOrderTDao.save(userordert);

		} catch (Exception e) {
			log.error("发布订单持久化用户信息出现异常！", e);
			super.setStatus(dataResponse, SystemCode.ERROR);
		}
	}

	/**
	 * 
	 * @Title: convertBean
	 * @Description:发布订单将客户端请求bean转换成
	 * @param reqPublishAboutCar
	 *            客户端数据请求bean 
	 * @param userordert 数据保存bean
	 * @author cheshaowei
	 * @throws
	 * 
	 */
	public void convertBean(ReqPublishAboutCar reqPublishAboutCar, Userordert userordert) {

		// 下单城市
		userordert.setPayCity(reqPublishAboutCar.getPayCity());
		// 发布者ID
		userordert.setPlaceOrderUserId(reqPublishAboutCar.getPlaceOrderUserId());
		// 下单类型+用户角色类型=3
		userordert.setOrderType(3 - reqPublishAboutCar.getUserType());
		// 出发时间
		userordert.setSetOutTime(DateFormatUtil.yyyy_MM_dd_HHmmss(reqPublishAboutCar.getSetOutTime()));
		// 起点
		userordert.setStartAddress(reqPublishAboutCar.getStartAddress());
		// 起点经度
		userordert.setStartLongitude(reqPublishAboutCar.getStartLongitude());
		// 起点纬度
		userordert.setStartLatitude(reqPublishAboutCar.getStartLatitude());
		// 终点
		userordert.setEndAddress(reqPublishAboutCar.getEndAddress());
		// 终点经度
		userordert.setEndLongitude(reqPublishAboutCar.getEndLongitude());
		// 终点纬度
		userordert.setEndLatitude(reqPublishAboutCar.getEndLatitude());
		// 距离
		userordert.setDistance(reqPublishAboutCar.getDistance());
		// 红包
		userordert.setRedBag(reqPublishAboutCar.getRedBag());
		// 留言
		userordert.setMessage(reqPublishAboutCar.getMessage());
		// 人数
		userordert.setNumber(reqPublishAboutCar.getNumber());
		// 总金额
		userordert.setTotalMoney(reqPublishAboutCar.getTotalMoney());
		// 设置叮币默认值
		userordert.setPayMoney(new BigDecimal(0));
		// 设置支付宝支付金额
		userordert.setAlipayMoney(new BigDecimal(0));

	}

}
